import { ManagerService } from './../../../service/manager.service';
import { User } from 'src/app/domain/user';
import { SignInService, SignInTest } from './../../../service/sign-in.service';
import { SignInRecord } from './../../../domain/sign-in-record';
import { Component, OnInit } from '@angular/core';
import { withLatestFrom } from 'rxjs/operators';
import { AuthService } from '../../../service/auth.service';
/*

@Author:赵明洋

@Date: 2018-12-12

@员工打卡功能实现

@Last Modified time: 2018-12-16
*/
@Component({
  selector: 'app-sign-in',
  templateUrl: './sign-in.component.html',
  styleUrls: ['./sign-in.component.css']
})
export class SignInComponent implements OnInit {

  private manager: User;
  sid = AuthService.getUserId();

  SignInRecords: SignInRecord[];

  SignInTesta: SignInTest =
    {
      user_id: 0,
      day: '',
      sign_in: '',
      sign_out: ''

    }
  SignInTestb: SignInTest =
    {
      user_id: 0,
      day: '',
      sign_in: '',
      sign_out: ''

    }

  SignInRecord: SignInRecord =
    {
      id: 0,
      user_id: 1000,
      day: '',
      sign_in: '',
      sign_out: ''

    }

  constructor(private signInRecordService: SignInService, private managerService: ManagerService) {
    this.manager = {
      id: null,
      user_name: null,
      name: null,
      age: null,
      sex: null,
      department: null, //部门
      position: null, //职务
      annual_leave_length: null, //年假上限
      remain_annual_leave: null, //剩余年假
    };
  }

  date = new Date().toLocaleDateString();
  sign_in: string;
  sign_out: string;
  Sid: number;
  Sidb: number;

  createSignIn() {
    var mydate = new Date();
    var date = mydate.toLocaleDateString();
    var time = mydate.toLocaleTimeString();
    this.date = date.replace(/\//g, "-");
    this.sign_in = mydate.getHours() + time.substring(time.indexOf(":"), time.indexOf(":") + 6);
    this.SignInTesta.day = this.date;
    this.SignInTesta.sign_in = this.sign_in;
    this.SignInTesta.user_id = this.sid;
    this.signInRecordService.getSignInRecord(this.date, this.sid)
      .subscribe(
        data => {
          if (data[0] == undefined) {
            this.signInRecordService.addSignInRecord(this.SignInTesta)
              .subscribe(data => {
                this.getSignInRecords(this.sid);
              });
            this.ngOnInit();
          }

        })

  }

  createSignOut() {
    var mydate = new Date();
    var date = mydate.toLocaleDateString();
    var time = mydate.toLocaleTimeString();
    this.date = date.replace(/\//g, "-");
    this.sign_out = mydate.getHours() + time.substring(time.indexOf(":"), time.indexOf(":") + 6);
    this.SignInTestb.day = this.date;
    this.SignInTestb.sign_out = this.sign_out;
    this.SignInTestb.user_id = this.sid;
    this.signInRecordService.getSignInRecord(this.date, this.sid)
      .subscribe(
        data => {
          this.SignInRecord = data[0];
          this.Sid = data[0].id;
          this.SignInTestb.sign_in = data[0].sign_in;
          this.signInRecordService.upDateSignInRecord(this.SignInTestb, this.Sid)
            .subscribe(data => {
              this.getSignInRecords(this.sid);
              console.log(data);
            });
          this.ngOnInit();
        }
      )

  }


  /* getRecord(date:string) :void
  {
    this.signInRecordService.getSignInRecord(date)
    .subscribe(
      data => {
        this.SignInRecord= data[0];
        console.log(this.SignInRecord);
      }
    )
      
  }
   */

  getSignInRecords(id: number): void {
    // this.LeaveRequests = this.leaverequeststate.getLeaveRequests();       //调用服务的方法获取请求信息
    this.signInRecordService.getSignInRecords(id)
      .subscribe(SignInRecords => {
        this.SignInRecords = SignInRecords;
        //按时间排序
        for (var i = 0; i < this.SignInRecords.length - 1; i++) {
          if (this.SignInRecords[i].day < this.SignInRecords[i + 1].day) {
            var temp: SignInRecord = this.SignInRecords[i];
            this.SignInRecords[i] = this.SignInRecords[i + 1];
            this.SignInRecords[i + 1] = temp;
            i = 0;
          }
        }
      });
  }

  ngOnInit() {

    this.getUser();

  }


  getUser() {
    var id = AuthService.getUserId();
    if (id == 0) {
      alert("请先登录");
    }
    else {
      this.managerService.getUser(id)
        .subscribe(manager => {
        this.manager = manager[0]; console.log("dsdsad");
          this.getSignInRecords(id);

        });
    }

  }

}

